package com.gitee.neuray.security.par.ParExamCate.biz;

import com.gitee.neuray.security.common.msg.ListRestResponse;
import com.gitee.neuray.security.common.msg.TableResultResponse;
import com.gitee.neuray.security.common.util.Query;
import com.gitee.neuray.security.par.ParExamPlan.entity.ParExamPlanEntity;
import com.gitee.neuray.security.par.basicData.biz.ParBasicDataBiz;
import com.gitee.neuray.security.par.basicData.entity.ParBasicDataEntity;
import com.gitee.neuray.security.par.basicData.mapper.ParBasicDataMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.common.Mapper;
import com.gitee.neuray.security.common.biz.BaseBiz;

import com.gitee.neuray.security.par.ParExamCate.mapper.ParExamCateMapper;
import com.gitee.neuray.security.par.ParExamCate.entity.ParExamCateEntity;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;


@Service
@Transactional(rollbackFor = Exception.class)
public class ParExamCateBiz extends BaseBiz<ParExamCateMapper,ParExamCateEntity> {
    @Override
    protected String getPageName() {
        return "ParExamCate"+"Biz";
    }

    @Resource
    private ParExamCateMapper parExamCateMapper;

    @Resource
    private ParBasicDataBiz parBasicDataBiz;

    @Resource
    private ParBasicDataMapper parBasicDataMapper;

    public ListRestResponse<List<ParExamCateEntity>> queryCate(Query query, ParExamCateEntity cateEntity){

        List<ParExamCateEntity> list = parExamCateMapper.queryCate(cateEntity);

        return new ListRestResponse("",list.size(),list);
    }

    public ListRestResponse<List<ParBasicDataEntity>> queryCateItem(Query query, ParBasicDataEntity basicDataEntity,String type,String deptCode){
        /*Example example = new Example(ParBasicDataEntity.class);
        Example.Criteria criteria = example.createCriteria();
        if("org".equals(type)){
            //如果是机关，考评类型作为查找键
            if("1".equals(basicDataEntity.getDwlx())){
                criteria.andEqualTo("dwlx",basicDataEntity.getDwlx());
                criteria.andEqualTo("kpdl",basicDataEntity.getKpdl());
                //如果是层级单位，id作为查找键
            }else{
                if("1".equals(basicDataEntity.getKpdl()) || "2".equals(basicDataEntity.getKpdl()) || "3".equals(basicDataEntity.getKpdl())){
                    criteria.andEqualTo("dwlx",basicDataEntity.getDwlx());
                    criteria.andEqualTo("kpdl",basicDataEntity.getKpdl());
                }else{
                    criteria.andEqualTo("id",basicDataEntity.getId());
                }
            }
        }else{
            criteria.andIsNull("dwlx");
            criteria.andEqualTo("kpdl",basicDataEntity.getKpdl());
            criteria.andEqualTo("parLevel","4");
        }

        example.orderBy("crtTime");
        example.orderBy("name");*/
        ParBasicDataEntity parBasicDataEntity = new ParBasicDataEntity();
        if("org".equals(type)){
            //如果是机关，考评类型作为查找键
            if("1".equals(basicDataEntity.getDwlx())){
                parBasicDataEntity.setDwlx(basicDataEntity.getDwlx());
                parBasicDataEntity.setKpdl(basicDataEntity.getKpdl());
                //如果是层级单位，id作为查找键
            }else{
                if("1".equals(basicDataEntity.getKpdl()) || "2".equals(basicDataEntity.getKpdl()) || "3".equals(basicDataEntity.getKpdl())){
                    parBasicDataEntity.setDwlx(basicDataEntity.getDwlx());
                    parBasicDataEntity.setKpdl(basicDataEntity.getKpdl());
                }else{
                    parBasicDataEntity.setId(basicDataEntity.getId());
                }
            }
        }else{
            parBasicDataEntity.setKpdl(basicDataEntity.getKpdl());
            parBasicDataEntity.setParLevel(4);
            parBasicDataEntity.setDeptCode(deptCode);
        }
        List<ParBasicDataEntity> list = parBasicDataMapper.queryCateItem(parBasicDataEntity);

        if(list != null && list.size() != 0){
            return new ListRestResponse("",list.size(),list);
        }else{
            parBasicDataEntity.setDeptCode(null);
            List<ParBasicDataEntity> listNoDept = parBasicDataMapper.queryCateItem(parBasicDataEntity);
            return new ListRestResponse("",listNoDept.size(),listNoDept);
        }


    }
}
